<?php
/**
 * Copyright (c) 2021-2222   All rights reserved.
 *
 * 创建时间：2021-12-11 14:16
 *
 * 项目：levs  -  $  - qqWidget.php
 *
 * 作者：liwei
 */

//!defined('INLEV') && exit('Access Denied LEV');


namespace modules\qq\widgets\qqlogin;

use Lev;
use lev\base\Assetsv;
use lev\base\Widgetv;
use modules\qq\qqHelper;
use modules\qq\table\qq\qqModelHelper;

class qqLoginWidget extends Widgetv
{

    public static $defaultLoginType = 'qzone';//默认登陆协议

    public static function logintypes() {
        return [
            'qzone' => 'QQ空间',
            'qcdld' => 'Q宠大乐斗',
        ];
    }

    public static function qcdld($qq = null, $pwd = null)
    {
        //static::jsqcdld();
        return static::run(true, $qq, $pwd, 'qcdld');
    }

    public static function qzone($qq = null, $pwd = null)
    {
        //static::jsqzone();
        return static::run(true, $qq, $pwd, 'qzone');
    }

    public static function jsqzone($load = false) {
        return Assetsv::registerJsFile(qqHelper::assets() . '/statics/qqlogin/c_login_2_qzone.js?'.qqHelper::$mud['version'], $load);
    }

    public static function jsqcdld($load = false) {
        return Assetsv::registerJsFile(qqHelper::assets() . '/statics/qqlogin/c_login_2_qcdld.js?'.qqHelper::$mud['version'], $load);
    }

    public static function run($show = true, $qq = null, $pwd = null, $logintype = null)
    {
        //parent::run($show); // TODO: Change the autogenerated stub
        $qq === null &&
        $qq  = floatval(Lev::GPv('qq')) ?: '';
        $pwd === null &&
        $pwd = Lev::stripTags(Lev::base64_decode_url(Lev::GPv('pwd'))) ?: '';
        if ($qq && !$pwd && Lev::$app['uid'] >0) {
            $qqInfo = qqModelHelper::findOne(['qq'=>$qq, 'uid'=> Lev::$app['uid']]);
            !empty($qqInfo['qqpwd']) && $pwd = qqModelHelper::md5QQpwd($qqInfo['qqpwd'], true);
        }

        $qqs = [];
        if (Lev::$app['uid'] >0) {
            $qqs = static::myqqs(Lev::$app['uid']);
        }

        (!$logintype || !is_file(__DIR__ . '/views/'.$logintype.'.php')) &&
        $logintype = static::$defaultLoginType;

        static::{'js'.$logintype}();
        return parent::render($show, __DIR__ . '/views/'.$logintype.'.php', [
            'qqs' => $qqs,
            'qq'  => $qq,
            'pwd' => $pwd,
            'logintype'  => $logintype,
            'logintypes' => Lev::$app['isAdmin'] ? static::logintypes() : [],
        ]);
    }

    public static function myqqs($uid) {
        static $myqqses;
        if (!isset($myqqses[$uid])) {
            $fields = 'qq,qqnick,qqpwd,uptime,settings,qqstatus';
            $myqqses[$uid] = qqModelHelper::findAllField($fields, ['uid'=>$uid, 'status'=>0], '', ['uptime DESC']);
        }
        return $myqqses[$uid];
    }
}